package com.asz.application
{

	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
			
            
	public class XMLLoadDataApplication extends Application
	{
		public function XMLLoadDataApplication()
		{
			super();
		}
		
		 
		 
		//private myColor:
		/**
		 * 
		 */
		protected function getUrlData():String
		{
			//"./data.xml"
			Alert.show("getUrlData to override");
			return null;
		}
		//private myColor:
		/**
		 * Sets up the application.
		 */
		protected function init():void
		{
			//load des data
			initData();
		}
		
		private function initData():void
	    {
			 //browserManager = BrowserManager.getInstance();
			 var loader:URLLoader = new URLLoader();
			configureListeners(loader);
			var request:URLRequest = new URLRequest(getUrlData());
			
			try {
				loader.load(request);
			} catch (error:Error) {
				Alert.show("Unable to load requested document."+error);
			}
	    }
	    private function configureListeners(dispatcher:IEventDispatcher):void {
		    dispatcher.addEventListener(Event.COMPLETE, completeHandler);
		    dispatcher.addEventListener(Event.OPEN, openHandler);
		    dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		    dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		    dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
		    dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		protected function createObjects():void{
		    Alert.show("createObjects to overrid e");
		}
		
		private function completeHandler(event:Event):void {
		    var loader:URLLoader = URLLoader(event.target);
		    var xmlFlow:XML = new XML(loader.data);
		    var sites:XMLList = xmlFlow.children();
		    trace (sites[0].toXMLString());
		    
			for each(var xmlsite:XML in sites) {
		    	var monSite:ASZSite = new ASZSite(xmlsite); 
		    	adSite(monSite);
			}
			createObjects();

		}
		
		//à surcharger
		protected function adSite(act:ASZSite):void {
				Alert.show("méthode adSite à surcharger");
		}
		
		
		private function openHandler(event:Event):void {
		    //Alert.show("openHandler: " + event);
		}
		
		private function progressHandler(event:ProgressEvent):void {
		    //Alert.show("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
		    Alert.show("securityErrorHandler: " + event);
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void {
		    //Alert.show("httpStatusHandler: " + event);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
		    Alert.show("ioErrorHandler: " + event);
		    }
		    
			private function doDragEnter(event:DragEvent):void 
            {
                DragManager.acceptDragDrop(Panel(event.target));
            }


		
	}
}